package com.swyc.osschat.Test;

import sun.misc.Unsafe;

/**
 * @description TODO
 * @Author: z156940l@outlook.com
 * @className Test
 * @Date: 2023/2/20 15:50
 */
public class Example {

    private volatile int state = 0;

    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long stateOffSet;

    static {
        try {
            stateOffSet = unsafe.objectFieldOffset(Example.class.getDeclaredField("state"));
        }catch (Exception e){
            throw new Error(e);
        }
    }

    public void doSomething(){
        if (unsafe.compareAndSwapInt(this, stateOffSet, 0, 1)){

        }
    }

}
